package com.qiujianwei.study.authcenter.service;

import com.qiujianwei.sdk.common.util.io.IoUtil;
import com.qiujianwei.study.authcenter.common.contract.Rsa.RsaProperties;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.IOUtils;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

@Component
public class InitRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {

        //初始化Rsa加密的公钥和私钥
        InputStream privateKeyStream = this.getClass().getClassLoader().getResourceAsStream("static/id_key_rsa");
        InputStream publicKeyStream = this.getClass().getClassLoader().getResourceAsStream("static/id_key_rsa.pub");
        if (Objects.nonNull(privateKeyStream) && Objects.nonNull(publicKeyStream)) {
            byte[] privateBytes = IoUtil.getBytesByInputStream(privateKeyStream);
            byte[] publicBytes = IoUtil.getBytesByInputStream(publicKeyStream);
            RsaProperties.privateKey = new String(privateBytes, StandardCharsets.UTF_8);
            RsaProperties.publicKey = new String(publicBytes, StandardCharsets.UTF_8);
        } else {
            System.out.println("公钥、私钥初始化失败");
        }
    }
}
